package love.yuanmengrobot.qianyugarden.controller;

import love.yuanmengrobot.qianyugarden.entity.ExperienceLog;
import love.yuanmengrobot.qianyugarden.entity.LoginLog;
import love.yuanmengrobot.qianyugarden.entity.PointsLog;
import love.yuanmengrobot.qianyugarden.entity.User;
import love.yuanmengrobot.qianyugarden.service.ExperienceLogService;
import love.yuanmengrobot.qianyugarden.service.LoginLogService;
import love.yuanmengrobot.qianyugarden.service.PointsLogService;
import love.yuanmengrobot.qianyugarden.service.UserService;
import love.yuanmengrobot.qianyugarden.utils.ResultObj;
import love.yuanmengrobot.qianyugarden.utils.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author zjj
 * @since 2021-07-13 14:00:42
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
      UserService userService;

    @Autowired
    PointsLogService pointsLogService;

    @Autowired
    ExperienceLogService experienceLogService;
    @Autowired
    LoginLogService loginLogService;
    /**
     * 通过主键查询单条数据
     *
     *
     * @return 单条数据
     */
    @RequestMapping(value = "/getUserInfoView")
    public ModelAndView getUserInfoView(HttpServletRequest request) {
        String objectid="UA_20210713150023049_72548";
        HttpSession session = request.getSession(false);
        if (session != null) {//
            User user=(User)session.getAttribute("UserInfo");
            if(user!=null){
                objectid=user.getObjectid();
            }
        }



        LoginLog loginLog = new LoginLog();
        loginLog.setUserid(objectid);
        List<LoginLog> loginLogs = loginLogService.queryAll(loginLog);
        PointsLog pointsLog = new PointsLog();
        ExperienceLog experienceLog = new ExperienceLog();
        experienceLog.setRelationid(objectid);
        pointsLog.setRelationid(objectid);
        List<PointsLog> pointsLogs = pointsLogService.queryAll(pointsLog);
        List<ExperienceLog> experienceLogs = experienceLogService.queryAll(experienceLog);
        User user = userService.queryById(objectid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("myinfo/myinfo");
        modelAndView.addObject("user",user);
        modelAndView.addObject("pointsLogs",pointsLogs);
        modelAndView.addObject("experienceLogs",experienceLogs);
        modelAndView.addObject("loginLogs",loginLogs);
        System.out.println(pointsLogs);
        System.out.println(experienceLogs);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = "/editUser",method = RequestMethod.POST)
    public ResultObj editUser(User user,HttpServletRequest request){
        System.out.println(user);
//        user.setObjectid("UA_20210713150023049_72548");
        HttpSession session = request.getSession(false);
        if (session != null) {//
            User userfind=(User)session.getAttribute("UserInfo");
            if(user!=null){
                user.setObjectid(userfind.getObjectid());
            }
        }
        if(userService.update(user)!=null){
            return new ResultObj(SystemConstant.OK,"修改成功");
        }else {
            return new ResultObj(SystemConstant.ERROR,"修改失败");
        }
    }

    @RequestMapping(value = "/uploadHeadportrait")
    public ResultObj updataHeadportrait(MultipartFile headportrait,HttpServletRequest request){
        User user = new User();
        user.setObjectid("UA_20210713150023049_72548");

        HttpSession session = request.getSession(false);
        if (session != null) {//
            User user2=(User)session.getAttribute("UserInfo");
            if(user2!=null){
                user.setObjectid(user2.getObjectid());
            }
        }


        if(userService.updateHeadportrait(headportrait,user)){
           return new ResultObj(SystemConstant.OK,"修改成功");
        }else {
           return new ResultObj(SystemConstant.ERROR,"修改失败");
        }
    }


  



}
